<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="司机名称" prop="driverName">
        <el-input
          style="width: 240px"
          v-model="queryParams.driverName"
          placeholder="请输入司机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="driverPhone">
        <el-input
          style="width: 240px"
          v-model="queryParams.driverPhone"
          placeholder="请输入司机手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          style="width: 240px"
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位" prop="storehouseLocationName">
        <el-input
          style="width: 240px"
          v-model="queryParams.storehouseLocationName"
          placeholder="请输入库位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="loadingType">
        <el-select v-model="queryParams.loadingType" placeholder="请选择类型" clearable style="width: 240px">
          <el-option v-for="loadingType in loadingTypeList" :key="loadingType.value" :label="loadingType.label" :value="loadingType.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 240px">
          <el-option v-for="status in statusList" :key="status.value" :label="status.label" :value="status.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预约日期">
        <el-date-picker
          clearable
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vqms:queue:reservation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vqms:queue:reservation:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vqms:queue:reservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="queueReservationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属工厂" align="center" prop="factoryName" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="司机名称" align="center" prop="driverName" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="司机电话" align="center" prop="driverPhone" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="车牌号" align="center" prop="licensePlate" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="库位" align="center" prop="storehouseLocationName" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="装卸货单号" align="center" prop="loadingNo" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="类型" align="center" prop="loadingType" width="150">
        <template v-slot="scope">
          <span v-if="scope.row.loadingType === 1">卸货</span>
          <span v-if="scope.row.loadingType === 2">自卸</span>
          <span v-if="scope.row.loadingType === 3">自提</span>
          <span v-if="scope.row.loadingType === 4">退货</span>
          <span v-if="scope.row.loadingType === 5">其他</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="150">
        <template v-slot="scope">
          <span v-if="scope.row.status === 1" style="color:red">未签到</span>
          <span v-if="scope.row.status === 2" style="color:#00BFFF">已签到</span>
          <span v-if="scope.row.status === 3" style="color:green">已完成</span>
        </template>
      </el-table-column>
      <el-table-column label="预约时间" align="center" prop="createTime" :show-overflow-tooltip="true"  width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="签到时间" align="center" prop="signTime" :show-overflow-tooltip="true"  width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="completeTime" :show-overflow-tooltip="true"  width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vqms:queue:reservation:update']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vqms:queue:reservation:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排队预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12" >
            <el-form-item label="所属工厂" prop="factoryName">
              <el-autocomplete
                clearable
                style="width:100%;"
                v-model="form.factoryName"
                value-key="name"
                :fetch-suggestions="querySearchAsync"
                placeholder="请输入所属工厂"
                @select="handleSelectFactory">
              </el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="司机名称" prop="driverName">
              <el-autocomplete
                clearable
                style="width:100%;"
                v-model="form.driverName"
                value-key="name"
                :fetch-suggestions="querySearchAsync2"
                placeholder="请输入司机名称"
                @select="handleSelectDriver">
              </el-autocomplete>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号" prop="driverPhone">
              <el-input v-model="form.driverPhone" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌号" prop="licensePlate">
              <el-input v-model="form.licensePlate" placeholder="请输入车牌号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="库位" prop="storehouseLocationName">
              <el-input v-model="form.storehouseLocationName" placeholder="请输入库位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="装卸货单号" prop="loadingNo">
              <el-input v-model="form.loadingNo" placeholder="请输入装卸货单号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型" prop="loadingType">
              <el-select v-model="form.loadingType" placeholder="请选择类型" clearable >
                <el-option v-for="loadingType in loadingTypeList" :key="loadingType.value" :label="loadingType.label" :value="loadingType.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态" clearable >
                <el-option v-for="status in statusList" :key="status.value" :label="status.label" :value="status.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectFactoryList } from "@/api/vqms/factory";
import { selectDriverList } from "@/api/vqms/driver";
import { listQueueReservation,getQueueReservation,addQueueReservation,updateQueueReservation,delQueueReservation } from "@/api/vqms/queueReservation";

export default {
  //name: "QueueReservation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排队预约表格数据
      queueReservationList: [],
      // 类型
      loadingTypeList :[
        {
          label:"卸货",
          value:1
        },
        {
          label:"自卸",
          value:2
        },
        {
          label:"自提",
          value:3
        },
        {
          label:"退货",
          value:4
        },
        {
          label:"其他",
          value:5
        }
      ],
      // 状态
      statusList :[
        {
          label:"未签到",
          value:1
        },
        {
          label:"已签到",
          value:2
        },
        {
          label:"已完成",
          value:3
        },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoryName: [
          { required: true, message: "所属工厂不能为空", trigger: "change" }
        ],
        driverName: [
          { required: true, message: "司机名称不能为空", trigger: "blur" }
        ],
        driverPhone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        licensePlate: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        storehouseLocationName: [
          { required: true, message: "库位不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排队预约列表 */
    getList() {
      this.loading = true;
      listQueueReservation(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.queueReservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$set(this.form, "loadingType", 1);
      this.$set(this.form, "status", 2);
      this.open = true;
      this.title = "添加排队预约";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getQueueReservation(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排队预约";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateQueueReservation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQueueReservation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('删除预约会同时删除排队记录，是否确认删除？').then(function() {
        return delQueueReservation(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vqms/queue/reservation/export', {
        ...this.queryParams
      }, `排队预约记录.xlsx`)
    },
    //所属工厂异步搜索
    async querySearchAsync(queryString, cb) {
      this.factoryList=[];
      let query={
        name : queryString
      }
      await selectFactoryList(query).then(response => {
        this.factoryList = response.data
      });
      var results = queryString ? this.factoryList.filter(this.createStateFilter(queryString)) : this.factoryList;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 1000 * Math.random());
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.name.indexOf(queryString) >= 0);
      };
    },
    handleSelectFactory(item) {
      this.form.factoryName=item.name
      this.form.factoryId=item.id
    },
    //司机异步搜索
    async querySearchAsync2(queryString, cb) {
      this.driverList=[];
      if(queryString !='' && queryString != undefined){
        let query={
          name : queryString
        }
        await selectDriverList(query).then(response => {
          this.driverList = response.data
        });
      }
      var results = queryString ? this.driverList.filter(this.createStateFilter(queryString)) : this.driverList;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 1000 * Math.random());
    },
    handleSelectDriver(item) {
      this.form.driverName=item.name
      this.form.driverId=item.id
      this.$set(this.form, "driverPhone", item.phone);
      this.$set(this.form, "licensePlate", item.licensePlate);
    },
  }
};
</script>
